<?php

/**
 * Description of updatecomment
 *
 * @author libaoling
 * @time 2015-1-20
 */
class UpdatecommentApp extends BackendApp
{
    private $_commend_mod;

    public function __construct()
    {
        parent::__construct();
        $this->_commend_mod = m("comment");
    }

    public function index()
    {
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'comment.goods_id',
                'name' => 'goods_id',
                'type' => 'int',
            ),
            array(
                'field' => 'comment.comment',
                'name' => 'comment',
                'equal' => 'like',
            )
        ));
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc')))
            {
                $sort = 'comment_id';
                $order = 'desc';
            }
        }
        else
        {
            $sort = 'comment_id';
            $order = 'desc';
        }
        $sort = $sort == "price" ? "g.{$sort}" : "comment.{$sort}";
        
        $page = $this->_get_page();

        $list = $this->_commend_mod->find(array(
            'conditions' => "order_id = 0" . $conditions,
            'fields' => 'this.*, g.price',
            'join' => 'belongs_to_goods',
            'count' => true,
            'limit' => $page['limit'],
            'order' => "{$sort} {$order}"
        ));

        $page['item_count'] = $this->_commend_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->assign("list", $list);
        
        $this->import_resource(array('script' => 'jqtreetable.js,inline_edit.js'));
        $this->display("update_comment_index.html");
    }
    
    public function add()
    {
        if (!IS_POST)
        {
            $this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
            $this->display("update_comment_form.html");
        }
        else
        {
            $data = array();
            $contents = explode("\n", $_POST['content']);
            foreach ($contents as $key => $content)
            {
                if($content){
                    $data[$key]['quantity'] = $this->_get_rand_num();
                    $data[$key]['evaluation'] = $this->_get_rand_score();
                    $data[$key]['from_user_name'] = $this->_get_rand_user_name();
                    $data[$key]['add_time'] = $this->_get_rand_time();
                    $data[$key]['goods_id'] = intval($_POST['goods_id']);
                    $data[$key]['comment'] = strip_tags($content);
                }
            }
            if (!$this->_commend_mod->add($data))
            {
                $this->show_warning($this->_commend_mod->get_error());
                return;
            }
            $this->_commend_mod->delete_cache($_POST['goods_id']);

            $this->show_message('添加成功', 'back_list', 'index.php?app=updatecomment', '继续添加', 'index.php?app=updatecomment&amp;act=add'
            );
        }
    }

    //异步修改数据
    function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();

        if (in_array($column, array('evaluation', 'quantity', 'comment', 'add_time')))
        {
            $data[$column] = $value;
            if($column == "add_time"){
                $data[$column] = strtotime($value);
            }
            $goods_ids = $this->_commend_mod->getCol("select goods_id from wl_comment where comment_id='{$id}'");
            $this->_commend_mod->edit($id, $data);
            if (!$this->_commend_mod->has_error())
            {
                $this->admin_log("异步修改伪造评论" . Lang::get($column) . ':' . $id);
                $this->_commend_mod->delete_cache($goods_ids[0]);
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return;
        }
        return;
    }
    
    public function drop()
    {
        $comment_ids = isset($_GET['id']) ? $_GET['id'] : 0;
        if (!$comment_ids)
        {
            $this->show_warning('请选择要删除的数据');

            return;
        }
        $ids = explode(',', $comment_ids); //获取一个类似array(1, 2, 3)的数组
        if (!$this->_commend_mod->drop($ids))    //删除
        {
            $this->show_warning($this->_commend_mod->get_error());

            return;
        }

        $this->show_message('删除成功');
    }
    
    private function _get_rand_score()
    {
        $arr = array(5, 5, 4, 5, 5);
        $i = rand(0, 4);
        return $arr[$i];
    }

    private function _get_rand_num()
    {
        $arr = array(1, 1, 1, 1, 1, 1, 2, 3);
        $i = rand(0, 7);
        return $arr[$i];
    }

    private function _get_rand_user_name()
    {
        $result = "";
        $str = "qwertyuiopasdfghjklzxcvbnm";
        $i = rand(7, 12);
        for ($index = 0; $index < $i; $index++)
        {
            $j = rand(0, strlen($str) - 1);
            $result .= $str{$j};
        }
        return $result;
    }
    
    private function _get_rand_time()
    {
        if($_POST['add_time_from'] && $_POST['add_time_to']){
            $begin_time = $_POST['add_time_from'];
            $begin_time_s = substr($begin_time, -2);
            $begin_time_s = $begin_time_s + 1;
            $begin_time_d = substr($begin_time, 0, strlen($begin_time) - 2);
            $begin_time_1 = $begin_time_d . $begin_time_s;

            $end_time = $_POST['add_time_to'];
            $day = (strtotime($end_time) - strtotime($begin_time))/(60 * 60 * 24);
            $date = rand(0, $day); // 31 + 12
        }else{
            $begin_time = date("Y-m-d");
            $begin_time_s = substr($begin_time, -2);
            $begin_time_s = $begin_time_s + 1;
            $begin_time_d = substr($begin_time, 0, strlen($begin_time) - 2);
            $begin_time_1 = $begin_time_d . $begin_time_s;
            
            $date = 0;
        }
        
        $begin = strtotime("{$begin_time} 08:00:00");
        $end = strtotime("{$begin_time_1} 01:00:00");
        $timestamp = rand($begin, $end) + $date * 24 * 3600;
        return $timestamp;
    }
}
